﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Dao.RepositoryBase;
using Domain.DomainBase;

namespace Server.ManagerBase
{
    public abstract class AbstractManager<T> : IManagerBase<T> where T : class
    {
        public IRepository<T> Dao { get; set; }

        public virtual T Get(object id)
        {
            return this.Dao.Get(id);
        }

        public virtual object Save(T model)
        {
            IDomainEntity entity = model as IDomainEntity;
            if (entity != null)
            {
                entity.CreateTime = DateTime.Now;
                entity.EditTime = DateTime.Now;
                entity.CreateUser = "LiuDong";
                entity.EditUser = "LiuDong";
            }
            return this.Dao.Save(model);
        }

        public void Update(T model)
        {
            IDomainEntity entity = model as IDomainEntity;
            if (entity != null)
            {
                entity.EditTime = DateTime.Now;
                entity.EditUser = "LiuDong";
            }
            this.Dao.Update(model);
        }

        public void SaveOrUpdate(T model)
        {
            IDomainEntity entity = model as IDomainEntity;
            if (entity != null)
            {
                if (entity.CreateTime <= DateTime.Parse("1900-01-01"))
                {
                    entity.CreateTime = DateTime.Now;
                }
                entity.EditTime = DateTime.Now;
                if (string.IsNullOrEmpty(entity.CreateUser))
                {
                    entity.CreateUser = "LiuDong";
                }
                entity.EditUser = "LiuDong";
            }
            this.Dao.SaveOrUpdate(model);
        }

        public virtual void Delete(T model)
        {
            this.Dao.Delete(model);
        }

        public void Delete(List<long> idList)
        {
            foreach (long item in idList)
            {
                T model = this.Dao.Get(item);
                if (model != null)
                {
                    this.Dao.Delete(model);
                }
            }
        }

        public virtual IQueryable<T> GetAll()
        {
            return this.Dao.GetAll();
        }

        public virtual IQueryable<T> GetPageList(int firstResult, int maxResult)
        {
            return this.Dao.GetPageList(firstResult, maxResult);
        }

        public virtual IQueryable<T> FindPageList(System.Reflection.PropertyInfo sort, string dir, int firstResult, int maxResult)
        {
            return this.Dao.FindPageList(sort, dir, firstResult, maxResult);
        }

        public virtual IQueryable<T> FindPageList(out long total, System.Reflection.PropertyInfo sort, string dir, int firstResult, int maxResult)
        {
            return this.Dao.FindPageList(out total, sort, dir, firstResult, maxResult);
        }
    }
}
